<!--审批流程设置-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button icon="el-icon-plus" v-waves :loading="downloadLoading" class="filter-item right-btn" type="primary" @click="newDialog=true">
        添加
      </el-button>
    </div>
    <ve-table
      :columns="columns"
      :table-data="tableData"
      row-key-field-name="id"
      :border-x="true"
      :border-y="true"
      :row-style-option="rowStyleOption"
      :checkbox-option="checkboxOptipon"
    />
    <div class="table-pagination">
      <ve-pagination
        :total="totalCount"
        :page-index="pageIndex"
        :page-size="pageSize"
        @on-page-number-change="pageNumberChange"
        @on-page-size-change="pageSizeChange"
      />
    </div>
    <el-dialog
      class="lnk-dialog"
      :close-on-click-modal="false"
      :title="dialogTitle"
      top="10vh"
      width="30%"
      :visible.sync="newDialog"
      :append-to-body="true"
    >
      <el-form
        ref="mainForm"
        :model="formData"
        :rules="rules"
        size="mini"
        class="editForm"
      >
        <!--表单项-->
        <el-row>
          <el-col>
            <el-form-item label="最小额度(元)" prop="minQuota" class="car-form-item">
              <el-input v-model="formData.minQuota" class="car-modal-col" placeholder="请输入最小额度(元)" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="最大额度(元)" prop="minQuota" class="car-form-item">
              <el-input v-model="formData.maxQuota" class="car-modal-col" placeholder="请输入最大额度(元)" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="审批等级" prop="approveLevel" class="car-form-item">
             <el-select v-model="formData.approveLevel" placeholder="请选择" clearable class="car-modal-col">
               <el-option
                 v-for="item in riskLevels"
                 :key="item.key"
                 :label="item.label"
                 :value="item.key"/>
             </el-select>
             </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="部门" required class="car-form-item" >
              <el-select
                v-model="serviceGroup"
                placeholder="请选择"
                clearable
                class="car-modal-col"
                @change="groupChange">
                <el-option
                  v-for="item in groupListData"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label"/>
              </el-select>
              </el-form-item>
              <el-form-item
                label="审核人"
                required
                class="car-form-item">
                <el-select
                  v-model="formData.userId"
                  placeholder="请选择"
                  clearable
                  class="car-modal-col">
                  <el-option
                    v-for="item in authUserData"
                    :key="item"
                    :label="item"
                    :value="item"/>
                </el-select>
              </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newDialog = false;$refs['mainForm'].resetFields();">取消</el-button>
        <el-button type="primary" @click="addData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
  .table-pagination {
    margin-top: 20px;
    text-align: right;
  }
  .right-btn{
    float: right;
  }
  .dialog-footer{
    text-align: center;
  }
  .car-modal-col{
    width: 280px;
  }
</style>

<script>
export default {
  data() {
    return {
      rules: {
        nickName: [
          {required: true, message: '必输', trigger: 'blur'}
        ]
      },
      appId:'',
      tableData: [],
      editorFlag: false,    // 编辑标志
      dialogTitle: '新增审批流程', // 弹框名称
      newDialog: false, // 添加弹框
      formData: {}, // 添加表单
      dataTable: [],
      riskLevels: [{ key: 'first', label: '一级' }, { key: 'second', label: '二级' }, { key: 'third', label: '三级' }],
      groupListData:[],
      authUserData:[],
      totalCount: '',
      serviceGroup:'',
      // page index
      pageIndex: 1,
      // page size
      pageSize: 10,
      listQuery: {},
      rowStyleOption: { // 表格样式
      stripe: true, // 斑马纹
      hoverHighlight: true, // hover高亮
      clickHighlight: true // 点击行高亮
      },
      checkboxOptipon: {
        // 行选择改变事件
        selectedRowChange: ({ row, isSelected, selectedRowKeys }) => {
          console.log(row, isSelected, selectedRowKeys)
        },
        // 全选改变事件
        selectedAllChange: ({ isSelected, selectedRowKeys }) => {
          console.log(isSelected, selectedRowKeys)
        }
      },
      columns: [
        { field: 'minQuota', key: 'a', title: '最小审批金额', align: 'center' },
        { field: 'maxQuota', key: 'b', title: '最大审批金额', align: 'center' },
        { field: 'approveLevel', key: 'c', title: '审批等级', align: 'center' ,
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            // 在这里写转换方法
            const list = this.riskLevels.filter((item) => { return item.key.toString() === row.approveLevel })
            return list[0].label
          }},
        { field: 'userId', key: 'd', title: '审批人', align: 'center' },
        { field: 'hobby', key: 'k', title: '操作',
          align: 'center',
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            return (
            <span>
              <el-button circle icon="el-icon-edit" class='filter-item' type={'primary'} on-click={()=>this.editorDealer(row)}></el-button>
              <el-button circle icon="el-icon-delete" class='filter-item' type={'danger'} on-click={()=>this.deleteorDealer(row)}></el-button>
            </span>
            )
          }
        }
      ]
    }
  },
  computed: {
  },
  created() {
    this.appId = sessionStorage.getItem("appId");
    this.initDatabase();
    this.initCommon();
  },
  methods: {
    // page number change
    pageNumberChange(pageIndex) {
      this.pageIndex = pageIndex
      this.initDatabase();
    },

    // page size change
    pageSizeChange(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.initDatabase();
    },

    async initCommon(){

      let param = { appId: this.appId }
      let data = await this.$http.get('/auth/user/groupUserDropDwon/' + param.appId)
      if (data.code === 200) {
        this.groupListData = data.data;
        const list = await this.dealDate(this.groupListData)
        this.groupListData = list
      }
    },

    async dealDate(data) {
      const list = []
      for (const item in data) {
        list.push({
          label: item,
          children: data[item]
        })
      }
      return list
    },

    /**
     * 初始化数据
     * @date 2021-05-10
     */
    async initDatabase() {
      this.tableData = [];
      let param = {appId: await sessionStorage.getItem("appId")};
      let data = await this.$http.get('/flow/riskApproveSet/getAll/'+this.appId, {});
      console.log(data);
      if (data.code === 200) {
        this.tableData = data.data;
        this.totalCount = data.data.length;
      } else {
        this.$message({
          type: 'error',
          message: '查询失败，请刷新页面'
        });
      }


    },

    /**
     * 部门选择
     * @date 2021/7/3
     * @param e
     */
    async groupChange(e) {
      this.authUserData = []
      this.$set(this.formData, 'userName', '')
      const list = await this.groupListData.filter((item) => {
        return item.label === e
      })
      if (list.length === 1) this.authUserData = list[0].children
    },
    /**
     * 编辑
     * @date 2021-05-08
     * @param row
     */
    editorDealer(row) {
      this.formData = row
      this.newDialog = true
      this.editorFlag = true // 编辑标志
      this.dialogTitle = '编辑风控设置'
    },
    /**
     * 点击新增按钮
     * @date 2021-05-08
     */
    toAdd() {
      this.formData = {}
      this.newDialog = true
      this.editorFlag = false // 编辑标志
      this.dialogTitle = '新增风控设置'
    },
    /**
     * 新增操作
     * @date 2021-05-08
     */
    async addData() {
      this.$refs['mainForm'].validate(async (valid) => {
        if (valid) {
            this.formData.appId = this.appId;
            let data = await this.$http.post('/flow/riskApproveSet/saveAllRiskApproveSet', this.formData)
            console.log(data);
            if (data.code === 200) {
              this.newDialog = false;
              this.initDatabase();
            } else {
              this.$message({
                type: 'info',
                message: '操作失败，请重试'
              });
            }
        }
      })
    }
  }
}
</script>
